//
//  TigerAppDelegate.m
//  Tiger
//
//  Created by King on 11-9-9.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "TigerAppDelegate.h"
#import "CustomTabBarViewController.h"


static float      p_valuearr[8] ={
    0.06f,-0.045f,0.033f,-0.023f,0.015f,-0.009f,0.005f,0.0f
};

static BOOL L0AccelerationIsShaking(UIAcceleration* last, UIAcceleration* current, double threshold) {
    double
    deltaX = fabs(last.x - current.x),
    deltaY = fabs(last.y - current.y),
    deltaZ = fabs(last.z - current.z);
    
    return
    (deltaX > threshold && deltaY > threshold) ||
    (deltaX > threshold && deltaZ > threshold) ||
    (deltaY > threshold && deltaZ > threshold);
}

@implementation TigerAppDelegate

@synthesize window;
@synthesize customviewController;
@synthesize lastAcceleration;

#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after application launch.
    [self CreatePopView];
	// Set the view controller as the window's root view controller and display.
    coverViewController = [[CoverViewController alloc] init];
	[window addSubview:coverViewController.view];
    //self.window.rootViewController = self.coverViewController;
    [self.window makeKeyAndVisible];
	
	NSFileManager *fileManager = [NSFileManager defaultManager];
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"noteData.plist"];
	BOOL success = [fileManager fileExistsAtPath:writableDBPath];
	
	if (!success) {
		NSString *defaultDBPath = [[NSBundle mainBundle] pathForResource:@"noteData" ofType:@"plist"];
		success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:nil];
	}
	
	[UIAccelerometer sharedAccelerometer].delegate = self;
	

    return YES;
}



- (void)applicationWillResignActive:(UIApplication *)application {
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    /*
     Called as part of  transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
     */
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */
}


- (void)applicationWillTerminate:(UIApplication *)application {
    /*
     Called when the application is about to terminate.
     See also applicationDidEnterBackground:.
     */
}
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
       [UIAccelerometer sharedAccelerometer].delegate = self;
}

#pragma mark -
#pragma mark UIAccelerometerDelegate
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
    if (self.lastAcceleration) {
        if (!histeresisExcited && L0AccelerationIsShaking(self.lastAcceleration, acceleration, 0.7)) {
            histeresisExcited = YES;
            
            /* SHAKE DETECTED. DO HERE WHAT YOU WANT. */
            
        } else if (histeresisExcited && !L0AccelerationIsShaking(self.lastAcceleration, acceleration, 0.2)) {
            histeresisExcited = NO;
        }
    }
    
    self.lastAcceleration = acceleration;
    
    if (histeresisExcited) {
        if (m_popView != nil && !m_popView.superview) {
            [self.window addSubview:m_popView];
            p_AnimateVCut = 0;
            [self popAnimate];
        }
    }
}
#pragma mark -
#pragma mark Memory management

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
    /*
     Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
     */
}

-(void) popAnimate
{
    NSLog(@"%f",p_AnimateValue);//p_valuearr
    p_AnimateValue = p_valuearr[p_AnimateVCut];
    p_AnimateVCut++;
    if (p_AnimateVCut > 7) {
        return;
    }
    CGAffineTransform tTargTrans = CGAffineTransformMakeRotation(p_AnimateValue);
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(popAnimate)];
    m_popView.transform =tTargTrans;
    [UIView commitAnimations];
}

- (void)CreatePopView
{
    m_popView = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height)];
    [m_popView setBackgroundColor:[UIColor clearColor]];
    [m_popView addTarget:self action:@selector(chagebutton) forControlEvents:UIControlEventTouchUpInside];
    UIImageView* popimageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, 410)];
    [popimageview setImage:[UIImage imageNamed:@"pop.png"]];
    [m_popView addSubview:popimageview];
    [popimageview release];
}
- (void)chagebutton
{
    if (m_popView.superview) {
        [m_popView removeFromSuperview];
    }
}
- (void)dealloc {
    [customviewController release];
	[coverViewController release];
    [window release];
    [m_popView release];
    [super dealloc];
}


@end
